﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace oqis.core.dbaccess
{
    public class SQLiteAccess : OQISDBAccessBase
    {
        #region 类成员变量

        ///// <summary>
        ///// 数据连接对象
        ///// </summary>
        //private SQLiteConnection _sqlConn = null;

        //private SQLiteCommand _sqlCMD = null;

        //private SQLiteDataAdapter _sqlDBAdapter = null;

        #endregion

        #region 类构建与初始化

        /// <summary>
        /// 类构建--构建数据库连接对象
        /// </summary>
        /// <param name="sourceFile">数据源文件</param>
        public SQLiteAccess(string sourceFile)
        {
            //this._sqlConn = CreateSQLiteConnection(sourceFile, "", false, false);
            CreateSQLiteConnection(sourceFile, "", false, false);
        }

        /// <summary>
        /// 构建数据库连接对象
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="password">密码</param>
        public SQLiteAccess(string sourceFile, string password)
        {
            //_sqlConn = CreateSQLiteConnection(sourceFile, password, false, false); 
            CreateSQLiteConnection(sourceFile, password, false, false);
        }

        /// <summary>
        /// 构建数据库连接对象
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="password">密码</param>
        /// <param name="useUTF">UTF格式</param>
        /// <param name="readOnly">只读</param>
        public SQLiteAccess(string sourceFile, string password, bool useUTF, bool readOnly) { CreateSQLiteConnection(sourceFile, password, useUTF, readOnly); }

        #endregion

        #region 类方法
 

        /// <summary>
        /// 更新表数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="table">数据</param>
        /// <returns>更新结果</returns>
        public override bool UpdateTable(string tableName, DataTable table)
        {
            // 判断 
            if (table == null || table.Rows.Count <= 0) return false;

            // 打开连接
            SQLiteDataAdapter adapter = new SQLiteDataAdapter("select * from " + tableName, (SQLiteConnection)this._dbConnection);
            SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(adapter);

            //// 得到数据表
            DataSet ds = new DataSet();

            // 更新数据
            adapter.Fill(ds, tableName);
            foreach (DataRow row in table.Rows)
            {
                ds.Tables[0].Rows.Add(row.ItemArray);
            }

            try
            {
                // 更新到数据库中
                int i = adapter.Update(ds, tableName);
                return Convert.ToBoolean(i);
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 类属性
 

        /// <summary>
        /// 返回当前数据库的类型
        /// </summary>
        public override  qiaoDBMSType DBMSType { get { return  qiaoDBMSType.SQLite; } }

        #endregion

        #region 内部方法

        /// <summary>
        /// 构建数据库连接对象
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="password">密码</param>
        /// <param name="useUTF">UTF格式</param>
        /// <param name="readOnly">只读</param>
        void CreateSQLiteConnection(string sourceFile, string password, bool useUTF, bool readOnly)
        {
            // 连接字符串
            string connString = "Data Source=" + System.IO.Path.GetFullPath(sourceFile) + ";Version=3;";

            // 密码
            if (password != string.Empty) { connString += "Password=" + password + ";"; }

            // 使用UTF格式
            if (useUTF) { connString += "UseUTF16Encoding=True;"; }

            // 只读连接
            if (readOnly) { connString += "Read Only=True;"; }

            SQLiteConnection sqliteConn = new SQLiteConnection(connString);

            this._dbConnection = (SQLiteConnection)sqliteConn;
            this._dbCommand = this._dbConnection.CreateCommand();

            this._dbAdapter = new SQLiteDataAdapter((SQLiteCommand)this._dbCommand);
        }
 

        #endregion
    }
}
